package cn.edu.ujs.coor.controller;

import cn.edu.ujs.coor.request.auth.LoginRequest;
import cn.edu.ujs.coor.request.auth.RegisterRequest;
import cn.edu.ujs.coor.response.JsonResponse;
import cn.edu.ujs.coor.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 认证相关Controller
 *
 * @author 柳曾雄
 * @date 2018-07-16
 */
@RestController
@RequestMapping(value = "/auth")
public class AuthController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     * @param request
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public JsonResponse register (@Valid @RequestBody RegisterRequest request) {
        userService.register(request);
        return JsonResponse.success();
    }

    /**
     * 用户登录
     * @param request
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public JsonResponse login (@Valid @RequestBody LoginRequest request) {
        String token = userService.login(request);
        return JsonResponse.success(token);
    }
}
